﻿using JustTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using Microsoft.AspNet.Identity;

namespace JustTest
{
    public partial class Test : System.Web.UI.Page
    {   
        // load current test just one
        protected JustTest.Models.Test currentTest;
        private static JustTestDb context = new JustTestDb();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["id"] == null)
            {
                this.Response.Redirect("Tests.aspx");
            }

            int testId = int.Parse(Request.Params["id"]);

            // should deal with N+1 problems
            currentTest = context.Tests.Include(t=>t.Questions).Include(t=>t.Questions.Select(q=>q.Answers)).FirstOrDefault(t=>t.Id == testId);
            if (currentTest.Status != TestStatus.Active)
            {
                this.Response.Redirect("Tests.aspx");
            }
        }

        public IEnumerable<JustTest.Models.Question> RepeaterQuestions_GetData()
        {
            return currentTest.Questions;
        }

        protected void ButtonSubmitTest_Click(object sender, EventArgs e)
        {
            this.GridViewQuestions.Visible = false;
            this.ButtonSubmitTest.Visible = false;

            int testResult = GetTestResult();

            this.LiteralTestResult.Text += testResult.ToString() + " points.";
            this.LiteralTestResult.Visible = true;

            SaveTestResult(testResult);
        }

        private void SaveTestResult(int testResult)
        {
            var logedUser =  context.Users.Find(Context.User.Identity.GetUserId());
            var testTitle = currentTest.Title;
            var testResultModel = context.TestResults.FirstOrDefault(result => result.TestTitle == testTitle);

            if (testResultModel != null)
            {
                testResultModel.Result = testResult;
            }
            else
            {
                testResultModel = new JustTest.Models.TestResult()
                {
                    Result = testResult,
                    PassDate = DateTime.Now,
                    TestTitle = currentTest.Title,
                    User = logedUser
                };
                context.TestResults.Add(testResultModel);
            }

            context.SaveChanges();
        }

        private void GetTestResultModel
            (int testResult, JustTest.Models.ApplicationUser logedUser)
        {
            
        }

        private int GetTestResult()
        {
            int testResult = 0;
            foreach (GridViewRow row in this.GridViewQuestions.Rows)
            {
                RadioButtonList cbl = (RadioButtonList)row.FindControlRecursive("RadioButtonListAnswers");

                int answerValue;
                bool isNum = int.TryParse(cbl.SelectedValue, out answerValue);

                if (isNum)
                {
                    testResult += answerValue;
                }
            }
            return testResult;
        }
    }
}